package com.zb.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.config.GlobalExceptionHandler;
import com.zb.dto.DishDto;
import com.zb.dto.SetmealDto;
import com.zb.entity.*;
import com.zb.mapper.SetmealDishMapper;
import com.zb.mapper.SetmealMapper;
import com.zb.service.CategoryService;
import com.zb.service.SetmealDishService;
import com.zb.service.SetmealService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @Transactional
    public void addSetmeal(SetmealDto setmealDto) {
        //保存套餐
        this.save(setmealDto);
        List<SetmealDish> setmealDish = setmealDto.getSetmealDishes();

        setmealDish = setmealDish.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDish);

    }

    @Override
    public PageData getSetmealList(int page, int pageSize, String name) {

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(!StringUtils.isEmpty(name), Setmeal::getName, name);
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        IPage<Setmeal> setmealIPage = setmealMapper.selectPage(setmealPage, setmealLambdaQueryWrapper);
        List<SetmealDto> list = new ArrayList<>();
        BeanUtils.copyProperties(setmealIPage.getRecords(), list);
        list.stream().map(item -> {
            item.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return item;
        }).collect(Collectors.toList());
        PageData data = new PageData<Setmeal>();
        data.setPage((int) setmealIPage.getPages());
        data.setPageSize((int) setmealIPage.getSize());
        data.setTotal((int) setmealIPage.getTotal());
        data.setList(setmealIPage.getRecords());
        return data;
    }

    @Transactional
    @Override
    public void delSetmealByIds(List<Long> ids) throws Exception {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(setmealLambdaQueryWrapper);
        if(count > 0) {
            throw new Exception("套餐正在售卖，不能删除");
        }


        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }


}
